package com.lge.qcontact.service;

import com.lge.qcontact.threads.SyncronizeContactsThread;
import com.lge.qcontact.utils.MUtils;

import android.app.Service;
import android.content.Intent;
import android.os.CountDownTimer;
import android.os.IBinder;

/**
 * 
 * @author project
 * started 서비스를 구현.
 * 서버 db와 로컬 db를 동기화 하는 service.
 * 일정 주기로 서버 db와 로컬 db를 비교하여 변동 사항이 있을 시 로컬 db 업데이트.
 * 환경설정에서 자동 동기화 해제 시 동작하지 않음.
 */
public class SynchronizingService extends Service{

	private int syncTimeGap = 10000;//= 60000*60;
	private SyncronizeContactsThread syncThread;
	private boolean runService=true;
	private CountDownTimer _timer;
	
	@Override
	public void onCreate() {
		syncThread = new SyncronizeContactsThread(getApplicationContext());
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		runService = false;
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		
		MUtils.loge("syncService started by sync time : " + syncTimeGap);
		
		return START_STICKY;
	}
	
	

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	
	public void setSyncTime(int syncTime){
		syncTimeGap = syncTime;
	}
	
	public int getSyncTime(){
		return syncTimeGap;
	}
	
	public void stopTimer(){
		_timer.cancel();
	}


}
